import execjs
import requests
from lxml import html
import re

js_search=re.compile('var share_con =(.*);(.*)通用',flags=re.DOTALL)
headers = {
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36'
}


def miaopai_get_page_info(url_str):
    try:
        if 'https' in url_str:
            url_str=url_str.replace('https','http')
        page = requests.get(url_str, headers=headers).text
        page_info = html.fromstring(page)
        js = ''
        for i in page_info.cssselect('script'):
            if isinstance(i.text, str) and 'share_con' in i.text:
                js = i.text
        # print(js_search.search(js).groups(1)[0])
        exj = execjs.compile( 'var share_con='+js_search.search(js).groups(1)[0]+'; function text () { return  share_con}')
        req=exj.call('text')
        req['video']=page_info.cssselect('video')[0].get('src')
        return req
    except KeyError:
        return False


if __name__ == '__main__':
    a = miaopai_get_page_info('https://m.miaopai.com/show/channel/o5VV8VINcfHnHcsMBdpriYDTMfv8gR4PkoSJ9w__')
    print(a)
